﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Vibz.SharePoint.Entity;

namespace Vibz.SharePoint
{
    public class SPUser
    {
        UserGroup.UserGroup _uGroup;
        public SPUser(object siteUrl)
        {
            _uGroup = new UserGroup.UserGroup();
            _uGroup.Url = siteUrl.ToString() + "/_vti_bin/UserGroup.asmx";
            _uGroup.Credentials = System.Net.CredentialCache.DefaultCredentials;
        }
        public UserInfo GetUserInfo(string userCred)
        {
            XmlNode mainNode = _uGroup.GetUserInfo(userCred);

            if(mainNode == null)
                return null;

            XmlNode userInfo = mainNode.FirstChild;

            return new UserInfo(userInfo.Attributes["ID"].Value,
                userInfo.Attributes["Name"].Value,
                userInfo.Attributes["Email"].Value,
                bool.Parse(userInfo.Attributes["IsSiteAdmin"].Value));
        }
        public bool IsUserAdmin(string userCred)
        {
            try
            {
                return GetUserInfo(userCred).IsSiteAdmin;
            }
            catch(Exception exc)
            {
                return false;
            }
        }
    }
}
